﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using ITOMMS.Business;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ITOMMS.Web
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string errorMessage = String.Empty;
        public string ErrorMessage
        {
            get
            {
                return this.errorMessage;
            }
        }

        protected void imgBtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            bool IsExistUser = false;
            string userId = this.txtUserId.Value.Trim();
            string password = this.txtUserPass.Value.Trim();
            if (userId == String.Empty || password == String.Empty)
            {
                InitialTextBox();
                this.errorMessage = "用户名和密码不能为空！";
                return;
            }

            if (!IsValidateCode())
            {
                return;
            }
            StringBuilder sql = new StringBuilder();
            sql.Append("  state= " + PubEnums.DeleteState.Available.ToString("D"));
            List<IDataParameter> parameters = new List<IDataParameter>();
            sql.Append(" AND UserName like @UserName");
            parameters.Add(new SqlParameter("@UserName", userId));
            sql.Append(" AND PWD=@PWD");
            parameters.Add(new SqlParameter("@PWD", password));
            List<tbUserEntity> listuser = new tbUserEntity().GetAllByQuery(sql.ToString(), parameters); ;
            if (listuser.Count > 0)
            {
                IsExistUser = true;
                SessionHelper.UserID = listuser[0].ID;
                SessionHelper.UserRoleGroup = Enum.GetName(typeof(PubEnums.RoleGroup), listuser[0].RoleGroup);
                SessionHelper.UserCompanyID = SessionHelper.UserID;//因为用户表和公司表合并了
                SessionHelper.UserCompanyName = listuser[0].CompanyName;
                SessionHelper.UserName = listuser[0].UserName;
                SessionHelper.RealName = listuser[0].RealName;
                SessionHelper.Department = listuser[0].DepartmentID;
                SessionHelper.UserEntity = listuser[0];
            }


            if (IsExistUser == false)
            {
                this.txtUserPass.Value = "";
                this.errorMessage = "用户名或密码错误！";
                return;
            }

            UserLoginProcess.SetAuthCookie(HttpContext.Current, listuser[0].ID, listuser[0].UserName, listuser[0].PWD, listuser[0].RoleGroup.ToString());

            Response.Redirect("~/Main/default.aspx", true);
        }


        private bool IsValidateCode()
        {
            if (Session["ValidateCode"] == null)
            {
                errorMessage = "验证码错误,请刷新一下页面重新登陆！";
                return false;
            }
            if (this.txtVerCode.Value.ToUpper() == String.Empty || this.txtVerCode.Value == null)
            {
                errorMessage = "请输入验证码! ";
                return false;
            }
            if (this.txtVerCode.Value.ToLower() != Session["ValidateCode"].ToString().ToLower())
            {
                errorMessage = "验证码错误! ";
                return false;
            }

            return true;
        }

        protected void InitialTextBox()
        {
            this.txtUserId.Value = "";
            this.txtUserPass.Value = "";
            this.txtVerCode.Value = "";
        }

    }
}
